package com.spring.study.deepseek.impl;

import com.spring.study.deepseek.DeepSeek;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;


@Slf4j
@Service
@ConfigurationProperties(prefix = "ds")
public class DeepSeekImpl implements DeepSeek {

    private String apikey;

    public void setApikey(String apikey) {
        this.apikey = apikey;
    }

    @Autowired
    private WebClient.Builder builder;
    @Override
    public String send(String content) {
        log.info("====apikey:{}====",apikey);
        // 请求体
        String requestBody = "{\n" +
                "  \"model\": \"deepseek-chat\",\n" +
                "  \"messages\": [\n" +
                "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n" +
                "    {\"role\": \"user\", \"content\": \"" + content + "\"}\n" +
                "  ],\n" +
                "  \"stream\": false\n" +
                "}";
        // 使用 WebClient.Builder 构建 WebClient 实例
        WebClient webClient = builder.baseUrl("https://api.deepseek.com/chat/completions").build();  // 创建 WebClient 实例
        Mono<String> response = webClient
                .post()
                .header("Content-Type","application/json")
                .header("Authorization","Bearer "+apikey)
                .bodyValue(requestBody)  // 设置请求体
                .retrieve()  // 发起请求并获取响应
                .bodyToMono(String.class);  // 将响应转换为 Mono<String>

        return response.block();  // 阻塞等待返回（同步方式）
    }
}
